<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<style type="text/css">
			html,body{ background-color:#F7F8FA; }
			.add_friends .list{ background-color: #fff; padding: 0 15px; margin-top:10px;}
			.add_friends .list li{ position:relative; padding: 8px 0;}
			.add_friends .list li:not(:last-child)::after{ position:absolute; content: ""; left: 0px; right: 0; bottom: 0; border-bottom: 1px #d4d4d4 solid; transform: scaleY(0.5);}
			.add_friends .list img{ width: 35px; height: 35px; border-radius: 4px; margin-right: 10px;}
			.add_friends .list h4{ display:block; position: relative; font-size: 15px; font-weight: 400; color: #333; line-height: 20px;}
			.add_friends .list p{ font-size: 12px; color: #999; padding-top: 1px;}
			.add_friends .list a{ display:inline-block; margin-left: 8px; font-size: 13px; color: #9f9f9f; line-height: 25px; width: 70px; text-align: center;}
			.add_friends .list .tg{ color:#5D5D5D;}
			.add_friends .list .on01{ background-color:#F6F6F6; color: #3262F1; border-radius: 4px;}
			.sk-three-bounce {
			  margin: 0 auto;
			  width: 80px;
			  text-align: center; }
			  .sk-three-bounce .sk-child {
				width: 13.5px;
				height: 13.5px;
				background-color: #3262F1;
				border-radius: 100%;
				display: inline-block;
				-webkit-animation: sk-three-bounce 1.4s ease-in-out 0s infinite both;
						animation: sk-three-bounce 1.4s ease-in-out 0s infinite both; }
			  .sk-three-bounce .sk-bounce1 {
				-webkit-animation-delay: -0.32s;
						animation-delay: -0.32s; }
			  .sk-three-bounce .sk-bounce2 {
				-webkit-animation-delay: -0.16s;
						animation-delay: -0.16s; }

			@-webkit-keyframes sk-three-bounce {
			  0%, 80%, 100% {
				-webkit-transform: scale(0);
						transform: scale(0); }
			  40% {
				-webkit-transform: scale(1);
						transform: scale(1); } }

			@keyframes sk-three-bounce {
			  0%, 80%, 100% {
				-webkit-transform: scale(0);
						transform: scale(0); }
			  40% {
				-webkit-transform: scale(1);
						transform: scale(1); }
			}
			.display-none{ display:none; }
			.text-center { text-align: center !important; }
		</style>
	</head>
	<body>
		<div class="add_friends">
			<ul class="list" id="userlist">
			</ul>
			<script id="userlist_template" type="text/template">
			{{ for (var i = 0, len = it.length; i < len; i++) { }}
				<li>
					<div class="m_cell_hd"><img data-url="{{=it[i].avatar}}" onload="fnLoadImage(this)" src="../../image/defaultimg.png" onerror="javascript:this.src='../../image/defaultimg.png';" /></div>
					<div class="m_cell_hd m_cell_primary">
						<h4>{{=it[i].realname}}</h4>
					</div>
					<div class="m_cell_ft">
						{{? it[i].state == 0}}
						<a href="javascript:;" tapmode="btnpress" onclick="addfriend({{=it[i].id}});" class="on01">加为好友</a>
						{{?? it[i].state == 1}}
						<a href="javascript:;" class="on01">已添加</a>
						{{?? it[i].state == 2}}

						{{?}}
					</div>
				</li>
			{{ } }}
			</script>
		</div>
		<div class="text-center display-none" style="padding: 50px 0 !important;" id="divnodata">
			<div class="text-center"><img src="../../image/nodata.png" style="margin:0 auto;width:41px;height:auto;"></div>
			<p class="text-center" style="font-size:12px;color:#9f9f9f;margin-top:15px;">该用户不存在</p>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js" ></script>
	<script type="text/javascript" src="../../script/app.js" ></script>
	<script type="text/javascript" src="../../script/doT.js"></script>
	<script type="text/javascript">
		var inputid;
		var params = {
			where: {
				type: 'userlist',
				keyword: ''
			},
			page: 1
		};
		var UILoading;
		apiready = function() {
			api.parseTapmode();
			UILoading = api.require('UILoading');
			$api.css($api.dom('.add_friends'), 'display: none');

			api.addEventListener({
					name:"scrolltobottom"
			},function(ret,err){
				params.page = page;
				getJsonFromAPI(params)
			});
		}
		function addfriend(uid) {
			openNavWin('#ddd','sendinvite_win','sendinvite_frm.html','朋友验证',false,false,true,[{text:'发送',color:'#3262F1',fontSize:14}],{dfuid:uid});
		}
		function fnLoadImage(obj) {
			obj.onload = null;
			var dataUrl = $api.attr(obj, 'data-url');
			if(dataUrl!=''){
				api.imageCache({
					url: dataUrl
				}, function(ret, err) {
					if(ret && ret.status == true){
						obj.src = ret.url;
					}
				});
			}
		}
		//win传递键盘ID
		function setInputid(id)
		{
			inputid = id;
			//alert(inputid+'?');
		}
		function getFilterJsonFromAPI(keyword) {
			showloading();
			page = 1;
			params.page = 1;
			params.where.keyword = keyword;
			getJsonFromAPI();
		}
		function getJsonFromAPI() {
			//从接口获取查询数据
			if(params.where.keyword=='')
			{
				return false;
			}
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret.value) {
					var userinfo = JSON.parse(ret.value);
					console.log(ApiUrl + '/v1/search/'+ $api.jsonToStr(params));
					api.ajax({
						url: ApiUrl + '/v1/search/'+ $api.jsonToStr(params),
						method: 'get',
						cache: false,
						timeout: 10,
						headers: {
							authorization: kAppKey+';'+userinfo.access_token+';'+userinfo.uid
						}
					}, function(ret, err) {
						hideloading();
						$api.css($api.dom('.add_friends'), 'display: block');
						if (ret) {
							if(ret.list.length > 0)
							{
								$api.addCls($api.byId('divnodata'),'display-none');
								var isAppend = true;
								if(page==1)
								{
									isAppend = false;
								}
								page++;
								var userlist = ret.list;
								UpdateList('userlist','userlist_template',userlist,isAppend);
							}
							else
							{
								if(page==1)
								{
									UpdateList('userlist','userlist_template',ret.list,false);
									$api.removeCls($api.byId('divnodata'),'display-none');
								}
							}
						} else {
							if(err.statusCode==402)
							{
								Relogin();
							}
							else
							{
								console.log(JSON.stringify(err));
								toast('网络异常');
							}
						}
					});
				}
			});
		}
		//渲染数据
		function UpdateList(domId,templateId,jsondata,isAppend) {
			//console.log(JSON.stringify(jsondata));
			var act_content = $api.byId(domId);
			var tempFn = doT.template($api.byId(templateId).innerHTML);
			var resultText = tempFn(jsondata);
			//alert(isAppend);
			if(isAppend)
			{
				$api.append(act_content, resultText);
			}
			else
			{
				$api.html(act_content, resultText);
			}
			//console.log(resultText);
			api.parseTapmode();
		}
	</script>
</html>